package com.abss.vote.config;

import com.abss.vote.model.MpInfo;
import com.abss.vote.service.MpInFoService;
import com.abss.vote.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 程序启动时将配置文件的信息写入数据库
 */
@Component
public class MyApplicationRunner implements ApplicationRunner {

    @Autowired
    MpInFoService mpInFoService;

    @Autowired 
    private JdbcTemplate jdbcTemplate;

    // 机构 图书馆 新的小程序
    private final String APP_ID_B_CLIENT_NEW = "wx08b26b1306ea997b";
    private final String APP_SECRET_B_CLIENT_NEW = "d6ac941cb1b17148c673558595f9cb8e";

    @Override
    public void run(ApplicationArguments var1) throws Exception{
        //System.out.println("程序开始运行，将配置文件信息写入数据库");
        try {
            String sql = "select * from [classificationofreading].[dbo].[mp_info] where mp_app_id=?";
            Map<String, Object> mpInfoMap = jdbcTemplate.queryForMap(sql,APP_ID_B_CLIENT_NEW);
            MpInfo mpInfo = new MpInfo();
            List<String> ls= FileUtils.getBanner();//获取配置文件中的数据
            mpInfo.setSERVER_ID(ls.get(0));
            mpInfo.setSERVER_NAME(ls.get(1));
            mpInfo.setMP_APP_ID(APP_ID_B_CLIENT_NEW);
            mpInfo.setMP_APP_SECRET(APP_SECRET_B_CLIENT_NEW);
            mpInfo.setWXCALLBACK(ls.get(4));
            mpInfo.setDOMAIN_NAME(ls.get(5));
            mpInfo.setFILE_PATH(ls.get(6));
            mpInfo.setACCESS_TOKEN(mpInfoMap.get("ACCESS_TOKEN")+"");
            mpInFoService.updateMpInFo(mpInfo);
        }catch (Exception e){
            e.printStackTrace();
        }
    }










}